package com.wyl.mobileplayer.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wyl.mobileplayer.R;
import com.wyl.mobileplayer.model.VideoModel;
import com.wyl.mobileplayer.model.bean.Media;
import com.wyl.mobileplayer.presenter.VideoPresenter;
import com.wyl.mobileplayer.ui.activity.SystemVideoPlayer;
import com.wyl.mobileplayer.ui.adapter.MediaAdapter;
import com.wyl.mobileplayer.ui.base.BaseFragment;
import com.wyl.mobileplayer.utils.LogUtils;

import java.io.File;
import java.util.List;

/**
 * 时间：2018/6/24 0024 13:08
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author WangYoule
 * @qq 270628297
 */
public class VideoFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener {

    private TextView tv_none;
    private ProgressBar progressBar;
    private RecyclerView recyclerView;

    private VideoPresenter videoPresenter;
    private List<Media> mMediaList;

    @Override
    protected void initView(View view) {
        tv_none = view.findViewById(R.id.tv_none);
        progressBar = view.findViewById(R.id.progressBar);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected int createLayout() {
        return R.layout.fragment_video;
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        videoPresenter = new VideoPresenter(this, new VideoModel(getActivity()));
        videoPresenter.getLocalVideo();

    }

    public void onSucceed(List<Media> mediaList) {
        LogUtils.d(mediaList.toString());
        mMediaList = mediaList;
        progressBar.setVisibility(View.GONE);
        if (mediaList.isEmpty()) {
            tv_none.setVisibility(View.VISIBLE);
        } else {
            MediaAdapter adapter = new MediaAdapter(getActivity(), mediaList);
            adapter.setOnItemClickListener(this);
            recyclerView.setAdapter(adapter);
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Media media = mMediaList.get(position);
        //1. 调用系统播放器
//        Intent intent = new Intent();
//        intent.setDataAndType(Uri.parse(media.data), "video/*");
//        startActivity(intent);

        //2.调用自己写的播放器
        Intent intent = new Intent(getActivity(), SystemVideoPlayer.class);
        intent.setDataAndType(Uri.parse(media.data), "video/*");
        startActivity(intent);

    }
}
